package com.kl.forum.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kl.forum.dao.*;
import com.kl.forum.entity.*;
import com.kl.forum.enums.ResultCode;
import com.kl.forum.util.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.kl.forum.util.ListUtil.randomTopic;

@Service
public class MemberService {
    @Autowired
    private  CircleArticleService circleArticleService;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private CircleArticleMapper circleArticleMapper;
    @Autowired
    private CircleDicMapper circleDicMapper;
    @Autowired
    private UserCoverUserMapper userCoverUserMapper;

    @Autowired
    private CircleLightMapper circleLightMapper;
    @Autowired
    private CircleCollectMapper  circleCollectMapper;
    @Autowired
    private  CirclePicMapper circlePicMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;





    public int countUserHealthyNum(int userId){
        Map map =new HashMap();
        map.put("userId",userId);
        return  memberMapper.countUserHealthyNum(map);
    }

    public List<Member> findUserByRecommend(Integer userId) {
        Map map=new HashMap();
//        int pageNum=1+(int) (Math.random() * 200);
//        PageHelper.startPage(pageNum, 50);
        if (userId!=null){
            map.put("userId",userId);
        }else {
            map.put("userId",0);
        }
        List<Member> list=memberMapper.findUserByRecommend(map);

//        Iterator<Member> it=list.iterator();
        if (list.size()>=10){
            list= randomTopic(list,10);
        }

        for (int i=0;i<list.size();i++){
            int count=memberMapper.findMyFans( list.get(i).getcId());
            list.get(i).setFans(count);
        }

        return list;


    }

    public PageInfo searchMemberName(String keyWord, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("keyWord",keyWord);
        List<Map> list=memberMapper.searchMemberName(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public ResultInfo findMyFans(Integer userId) {
        ResultInfo re=new ResultInfo();
        Map map=new HashMap();
        try {
            int count=memberMapper.findMyFans(userId);
            re.setData(count);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }

        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    public ResultInfo findMyFansList(Integer userId, Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("userId",userId);
        try {
            List<Member> list=memberMapper.findMyFansList(map);
            for(int i=0;i<list.size();i++){
                UserCoverUser userCoverUser= userCoverUserMapper.findOneUserCoverUser(userId,list.get(i).getcId());
                if (userCoverUser!=null){
                    list.get(i).setUserStatus(userCoverUser.getStatus());
                }else {
                    list.get(i).setUserStatus(0);
                }
            }

            PageInfo<Member> pageInfo = new PageInfo<>(list);
            re.setData(pageInfo);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    public ResultInfo findMyCircleArticle(Integer userId,Integer coverUserId, Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("userId",userId);
        try {
            List<CircleArticle> list=circleArticleMapper.findMyCircleArticle(userId);
            if (coverUserId!=null){
                if (list!=null&&list.size()>0) {
                    list = circleArticleService.setPic(list, coverUserId);
                }
            }else{
                if (list!=null&&list.size()>0) {
                    list = circleArticleService.setPic(list, userId);
                }
            }
            PageInfo<CircleArticle> data = new PageInfo<>(list);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
         e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findMyCircleArticleCount(Integer userId) {
        ResultInfo re=new ResultInfo();
        Map map=new HashMap();
        map.put("userId",userId);
        try {
            int data=circleArticleMapper.findMyCircleArticleCount(userId);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findMyFollowCount(Integer userId) {
        ResultInfo re=new ResultInfo();
        Map map=new HashMap();
        map.put("userId",userId);
        try {
            int data1=memberMapper.findMyFollowUserCount(userId);
            int data2=circleDicMapper.findMyFollowCircleCount(userId);
            re.setData(data1+data2);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findMyFollowList(Integer userId, Integer type,Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("userId",userId);
        try {
            if (type==1){
                List<Map> list1=memberMapper.findMyFollowUserList(map);
                PageInfo<Map> data1 = new PageInfo<>(list1);
                re.setData(data1);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }else{

                List<Map> list2=circleDicMapper.findMyFollowCircleList(map);
                PageInfo<Map> data2 = new PageInfo<>(list2);
                re.setData(data2);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    public ResultInfo findMyCollectList(Integer userId, Integer type, Integer pageNum, Integer pageSize) {

        ResultInfo re=new ResultInfo();
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("userId",userId);
        try {
            if (type==1){
                List<CircleArticle> list1=circleArticleMapper.findMyCollectCircleArticleList(map);
                circleArticleService.setPic(list1,userId);
                PageInfo<CircleArticle> data = new PageInfo<>(list1);
                re.setData(data);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


    public ResultInfo findMyCounts(Integer userId ) {
        ResultInfo re=new ResultInfo();
        try {
            //粉丝数量
            int fanscount=memberMapper.findMyFans(userId);
            //动态数量
            int articleCount=circleArticleMapper.findMyCircleArticleCount(userId);
            //用户  圈子 数量
            int followUserCount=memberMapper.findMyFollowUserCount(userId);
            int followCircleCount=circleDicMapper.findMyFollowCircleCount(userId);
            Map map=new HashMap();
            map.put("fanscount",fanscount);
            map.put("articleCount",articleCount);
            map.put("circleAndUser",followUserCount+followCircleCount);
            re.setData(map);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }

        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findUserFollowStatus(Integer userId, Integer coverUserId) {
        ResultInfo re=new ResultInfo();
        Map map=new HashMap();
        try {
            UserCoverUser coverUser=  userCoverUserMapper.findOneUserCoverUser(userId,coverUserId);
            Integer  followStatus;
            if (coverUser!=null){
                followStatus= coverUser.getStatus();
            }else {
                followStatus=0;
            }
            map.put("followStatus",followStatus);
            re.setData(map);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public int findBanStatus(Integer userId) {
     return    memberMapper.findBanStatus(userId);
    }

    public ResultInfo findUserId(String uuId) {
        ResultInfo re=new ResultInfo();
        try {
            Member m=  memberMapper.findUserByUuId(uuId);
            re.setData(m);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setData("系统错误");
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


//    public  List<CircleArticle> setPic(List<CircleArticle> list,Integer userId){
//
//        for (int i=0;i<list.size();i++){
//            if (userId!=null){
//                CircleLight circleLight= circleLightMapper.findOneCircleLight(list.get(i).getId(),userId);
//                if(circleLight!=null){
//                    list.get(i).setLightStatus(circleLight.getStatus());
//                }else {
//                    list.get(i).setLightStatus(0);
//                }
//
//                UserCoverUser userCoverUser= userCoverUserMapper.findOneUserCoverUser(userId,list.get(i).getUserId());
//                if (userCoverUser!=null){
//                    list.get(i).setUserStatus(userCoverUser.getStatus());
//                }else {
//                    list.get(i).setUserStatus(0);
//                }
//
//
//                CircleCollect circleCollect=  circleCollectMapper.findOneCircleCollect(userId,list.get(i).getId());
//                if (circleCollect!=null){
//                    list.get(i).setCollectStatus( circleCollect.getStatus());
//                }else {
//                    list.get(i).setCollectStatus(0);
//                }
//            }
//
//            //图片查询
//            List<CirclePic> circlePics= circlePicMapper.findPicByArticleId(list.get(i).getId());
//            if (circlePics!=null){
//                list.get(i).setPicList(circlePics);
//            }
//
//            if (!stringRedisTemplate.opsForHash().hasKey("circleArticleViewNumCount",list.get(i).getId().toString())){
//                stringRedisTemplate.opsForHash().put("circleArticleViewNumCount",list.get(i).getId().toString(),"0");
//            }
//            Object articleViewNumCount=stringRedisTemplate.opsForHash().get("circleArticleViewNumCount",list.get(i).getId().toString());
//            list.get(i).setViewNum(Integer.parseInt(articleViewNumCount.toString()));
//            if (!stringRedisTemplate.opsForHash().hasKey("circleArticleLightNumCount",list.get(i).getId().toString())){
//                stringRedisTemplate.opsForHash().put("circleArticleLightNumCount",list.get(i).getId().toString(),"0");
//            }
//            Object articleLightNumCount=stringRedisTemplate.opsForHash().get("circleArticleLightNumCount",list.get(i).getId().toString());
//            list.get(i).setLightNum(Integer.parseInt(articleLightNumCount.toString()));
//            int commentNum= circleArticleMapper.countCommentNum(list.get(i).getId());
//            int replyCommentNum  = circleArticleMapper.countReplyCommentNum(list.get(i).getId());
//            list.get(i).setCommentNum(commentNum+replyCommentNum);
//        }
//
//        return list;
//    }
}
